package com.whf.android.jar.base.latte;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.media.MediaManage;
import com.whf.android.jar.util.log.LatteLogger;

import qiu.niorgai.StatusBarCompat;


/**
 * 界面
 *
 * @author qf
 * @version 2020-03-23
 */
public abstract class BaseActivity extends AbstractActivity implements ActivityResultCallback<ActivityResult> {

    private static final String TAG_OFFSET = "TAG_OFFSET";
    private static final int KEY_OFFSET = -123;

    /*** 跳转界面回调刷新器 */
    protected ActivityResultLauncher<Intent> startLauncher;

    /**
     * 设置状态栏字体图标深色(true)或浅色(false)
     */
    protected boolean isDark() {
        return true;
    }

    /**
     * 内容是否与状态栏重叠。
     */
    protected boolean isStatusBar() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (setLayout() instanceof Integer) {
            int rootView = (int) setLayout();
            setContentView(rootView);
        } else if (setLayout() instanceof View) {
            View rootView = (View) setLayout();
            setContentView(rootView);
        } else {
            throw new ClassCastException("type of setLayout() must be in or View!");
        }
        //持有当前activity
        Latte.getConfigurator().withActivity(this);
        startLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this);
        onBindView(savedInstanceState);
        //透明状态栏（去掉阴影）
        StatusBarCompat.translucentStatusBar(this, true);
        //状态栏深色或浅色
        if (isDark()) {
            //SDK >= 23, 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
            StatusBarCompat.changeToLightStatusBar(this);
        } else {
            //将状态栏改为深色模式(状态栏 icon 和字体会变成浅色, 即默认模式)
            StatusBarCompat.cancelLightStatusBar(this);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (Latte.isProhibitScreenshot()) {
            if (MediaManage.getInstance().checkStoragePermission()) {
                MediaManage.getInstance().init(getContentResolver());
            } else {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        MediaManage.getInstance().unregister();
    }

    /**
     * 布局文件
     *
     * @return Object
     */
    protected abstract Object setLayout();

    /**
     * 初始化
     *
     * @param savedInstanceState:Bundle
     */
    public abstract void onBindView(@Nullable Bundle savedInstanceState);

    //******************************************************************************//

    /**
     * Add the top margin size equals status bar's height for view.
     *
     * @param view The view.(只能使用，不建议重构)
     */
    protected final void addMarginTopEqualStatusBarHeight(View view) {
        if (view == null) {
            return;
        }
        if (isStatusBar()) {
            return;
        }
        view.setTag(TAG_OFFSET);
        Object haveSetOffset = view.getTag(KEY_OFFSET);
        if (haveSetOffset != null && (Boolean) haveSetOffset) {
            return;
        }
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin,
                layoutParams.topMargin + getStatusBarHeight(),
                layoutParams.rightMargin,
                layoutParams.bottomMargin);
        view.setTag(KEY_OFFSET, true);
    }

    private int getStatusBarHeight() {
        int resourceId = Latte.getBarHeight();
        return resourceId == -1 ? 36 : resourceId;
    }

    //******************************************************************************//

    protected void setTextView(@IdRes int id, String text) {
        final TextView textView = findViewById(id);
        if (!TextUtils.isEmpty(text)) {
            textView.setText(text);
        }
    }

    protected void setOnClickListener(int id, View.OnClickListener l) {
        View textView = findViewById(id);
        if (textView != null) {
            textView.setOnClickListener(l);
        }
    }

    @Override
    public void outSignIn() {
        if (outSignInActivity() != null &&
                !getClass().getName().equals(outSignInActivity().getName())) {
            Intent intent = new Intent();
            intent.setClass(this, outSignInActivity());
            intent.putExtra(STATE_OUT, false);
            ActivityUtils.startActivity(intent);
            finish();
        } else {
            LatteLogger.e("未实现退出登录");
        }
    }

    @Override
    public void onActivityResult(ActivityResult result) {

    }

}
